package org.restql.db.operation;

import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.restql.webapp.RestQLException;
import org.restql.db.resource.DatabaseResource;
import org.restql.db.resource.RequestResource;

public class DatabaseDelete extends DatabaseOperation {

	private static Log logger = LogFactory.getLog(DatabaseDelete.class);

	public DatabaseDelete(DatabaseResource databaseResource, RequestResource requestResource) {
		super(databaseResource, requestResource);
	}

	public void executeSql(InputStream stream) throws RestQLException {
		try {
			String constructedSQL = this.databaseResource.getDeleteString(this.requestResource);

			// Execute SQL query
			PreparedStatement stmt =
				getConnection().prepareStatement(constructedSQL);

			logger.info("Executing: " + constructedSQL);
			stmt.executeUpdate();

		} catch (SQLException e) {
			throw new RestQLException("Error executing select query", e);
		}
	}

}
